const RedisService = require('../services/content_id_service');
const spider = require('../services/spider_service');
// [ '2833130', '908928', '163861' ]
switch (process.argv[2] || process.env.NODE_ARGV_2) {
    case 'generate_ids':
        RedisService.generateAcfunIdsToRedis(Number(process.argv[3]), Number(process.argv[4])).then(r => {
            console.log('done');
            process.exit(0);
        }).catch(e => {
            console.log(e);
            process.exit(0);
        })
        break;
    case 'get_single_article':
        spider.getSingleArticle(process.argv[3]).then(r => {
            console.log(r);
            process.exit(0);
        }).catch(e => {
            console.log(e);
            process.exit(0);
        });
        break;
    case 'start_getting_articles':
        getActiclesBG().then(r => {
                console.log(r);
                process.exit(0);
            }).catch(e => {
                console.log(e);
                process.exit(0);
            });
        // spider.spideringActicles(Number(process.argv[3])).then(r => {
        //     console.log(r);
        //     process.exit(0);
        // }).catch(e => {
        //     console.log(e);
        //     process.exit(0);
        // })
        break;

    default:
        process.exit(0);
        break;
}


async function getActiclesBG()
{
    const remainingCount = await RedisService.getRemainingIDcount();
    const numbersPertime = 5
    let errorCount = 0;
    let successCount = 0;
    while(remainingCount >= numbersPertime)
    {
       await spider.spideringActicles(numbersPertime).then(r => {
            console.log(r);
        }).catch(e => {
            console.log(e);
        })
    }
}